package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import pluginInterfaces.IRecommender;
import utils.Codes;
import utils.Messages;
import viewInterfaces.IViewOutputJarSetup;
import controller.ControllerOutputJarSetup;
import controllerInterfaces.IControllerOutputJarSetup;

/**
 * View used to establish the parameters of the output jar component.
 * 
 * @author Andy Cullache
 */
public class ViewOutputJarSetup extends JFrame implements IViewOutputJarSetup{

	private JPanel contentPane;
	private JTextField txtFile;
	private IControllerOutputJarSetup myController;
	private JComboBox<IRecommender> comboBox;
	private String dirPath;
	private String nameSave;

	/**
	 * Default constructor. Create the frame.
	 */
	public ViewOutputJarSetup() {
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentShown(ComponentEvent arg0) {
				updateListAlgorithms();
			}
		});
		setTitle("Generate Output (.Jar)");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 163);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JButton btnChoose = new JButton("Choose");
		btnChoose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setDirectory();
			}
		});
		
		txtFile = new JTextField();
		txtFile.setEditable(false);
		txtFile.setColumns(10);
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				createJarFile();
			}
		});
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		
		comboBox = new JComboBox();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(btnChoose)
							.addGap(33)
							.addComponent(txtFile, GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE))
						.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
							.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
							.addComponent(btnOk, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(btnCancel)))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnChoose)
						.addComponent(txtFile, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(18)
							.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addContainerGap(39, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnOk)
								.addComponent(btnCancel))
							.addContainerGap())))
		);
		contentPane.setLayout(gl_contentPane);
		
		if(comboBox.getItemCount()==0){
//			btnOk.setEnabled(false);
//			btnChoose.setEnabled(false);
		}
		getRootPane().setDefaultButton(btnOk);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
	}

	/**
	 * Algorithm updates the list of combobox from the list obtained from the controller of this view.
	 */
	private void updateListAlgorithms (){
		List<IRecommender> algorithms = myController.getRecommenderList();
		for(IRecommender recommender : algorithms){
			comboBox.addItem(recommender);
		}
	}
	
	/**
	 * Method used to select a file name and set the jar file directory of the attribute "nameLoad" and "dirpath" respectively.
	 * Set the text of file directory in this view.
	 */
	private void setDirectory(){
		String[] aux = myController.selectDirectory();
		this.nameSave = aux[0];
		this.dirPath = aux[1];
		this.txtFile.setText(this.dirPath);
	}
	
	/**
	 * Method used to display error messages with the respective code.
	 * 
	 * @param code      Error code.
	 * @param message      Message code.
	 */
	public void renderError(int code, String message) {
		javax.swing.JOptionPane.showMessageDialog(this, code + " - " + message, "Error",javax.swing.JOptionPane.ERROR_MESSAGE);	
	}

	/**
	 * Sets the controller of this view.
	 * 
	 * @param myController      Output jar controller.
	 */
	@Override
	public void setController(ControllerOutputJarSetup controllerOutputJar) {
		this.myController = controllerOutputJar;		
	}
	
	/**
	 * Invokes the controller  method that makes the writing of the jar, using as parameters the file 
	 * name and directory and recommendation algorithms that will be included in it.
	 */
	private void createJarFile() {
		IRecommender alg = (IRecommender) this.comboBox.getSelectedItem();
		if(alg != null){
			if(nameSave==null || dirPath==null){
				renderError(Codes.errorUnselectedDirectory, Messages.errorUnselectedDirectory);
				return;
			}
			myController.createJarOutput(nameSave,dirPath,(IRecommender) comboBox.getSelectedItem());
				this.dispose();
		}
		else{
			renderError(Codes.errorUnselectedTestResult, Messages.errorUnselectedTestResult);
		}
	}
	

	
}
